/**
 * 
 */
package com.ez.supervise.service.impl;

import java.util.List;
import java.util.Map;

import com.ez.supervise.core.Page;
import com.ez.supervise.dao.GoalCategoryDao;
import com.ez.supervise.model.biz.GoalCategory;
import com.ez.supervise.service.GoalCategoryService;

/**
 * @author searph.wang
 * @email searph.wang@gmail.com
 * @Created at 2009-7-22
 * 
 */
public class GoalCategoryServiceImpl implements GoalCategoryService {
	private GoalCategoryDao goalCategoryDao;

	// public int update(GoalCategory org) {
	// return sqlMapClientTemplate.update("sys.updateGoalCategory", org);
	// }

	public void setGoalCategoryDao(GoalCategoryDao goalCategoryDao) {
		this.goalCategoryDao = goalCategoryDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.base.BaseService#delete(java.lang.Long)
	 */
	public void delete(Long id) {
		GoalCategory node = (GoalCategory) goalCategoryDao.get(id);
		goalCategoryDao.delete(id);
		GoalCategory parent = goalCategoryDao.getNodeById(node.getParentId());
		if (parent != null) {
			List children = goalCategoryDao.getChildrenById(parent.getId());
			if (children == null
					|| children.isEmpty()
					&& !GoalCategory.FLAG_YES.equalsIgnoreCase(parent
							.getIsLeaf())) {
				parent.setIsLeaf(GoalCategory.FLAG_YES);
				goalCategoryDao.update(parent);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.base.BaseService#insert(java.lang.Object)
	 */
	public void insert(Object entity) {
		goalCategoryDao.insert(entity);
		GoalCategory node = (GoalCategory) entity;
		GoalCategory parent = goalCategoryDao.getNodeById(node.getParentId());
		if (parent != null) {
			node.setCategoryIdPath(parent.getCategoryIdPath() + "/"
					+ node.getId());
			node.setCategoryNamePath(parent.getCategoryNamePath() + "/"
					+ node.getName());
		} else {
			node.setCategoryIdPath(node.getId().toString());
			node.setCategoryNamePath(node.getName());
		}
		goalCategoryDao.update(node);
//不计算分分数
//		if (parent != null
//				&& GoalCategory.FLAG_YES.equalsIgnoreCase(parent.getIsLeaf())) {
//			parent.setIsLeaf(GoalCategory.FLAG_NO);
//			goalCategoryDao.update(parent);
//		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.base.BaseService#update(java.lang.Object)
	 */
	public void update(Object entity) {
		goalCategoryDao.update(entity);
		GoalCategory node = (GoalCategory) entity;
		GoalCategory parent = goalCategoryDao.getNodeById(node.getParentId());
		if (parent != null) {
			node.setCategoryIdPath(parent.getCategoryIdPath() + "/"
					+ node.getId());
			node.setCategoryNamePath(parent.getCategoryNamePath() + "/"
					+ node.getName());
		} else {
			node.setCategoryIdPath(node.getId().toString());
			node.setCategoryNamePath(node.getName());
		}
		if (node.isLeaf()) {
			goalCategoryDao.updatePercent(node.getId());
		}
		goalCategoryDao.update(node);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.base.BaseService#get(java.lang.Long)
	 */
	public Object get(Long id) {
		return (GoalCategory) goalCategoryDao.get(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ez.supervise.service.GoalCategoryService#findByPage(com.ez.supervise
	 * .core. Page)
	 */
	public Page findByPage(Page page) {
		page.setRoot(goalCategoryDao.findByPage(page));
		page.setTotalProperty(goalCategoryDao.findByCount(page));
		return page;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.GoalCategoryService#findAll()
	 */
	public List findAll() {
		return goalCategoryDao.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ez.supervise.service.GoalCategoryService#getChildrenById(java.lang
	 * .Long)
	 */
	public List getChildrenById(Long parentId) {
		return goalCategoryDao.getChildrenById(parentId);
	}

	public List<GoalCategory> getChildrenById(Long parentId, Long applyOrgId) {
		return goalCategoryDao.getChildrenById(parentId, applyOrgId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.GoalCategoryService#getRoots(int)
	 */
	public List getRoots(int year) {
		return goalCategoryDao.getRoots(year);
	}

	public GoalCategory getNodeById(Long nodeId) {
		return goalCategoryDao.getNodeById(nodeId);
	}

	public List findValidYear() {
		return goalCategoryDao.findValidYear();
	}

	public int countDetailByCategory(Long categoryId) {
		return goalCategoryDao.countDetailByCategory(categoryId);
	}

	public List findCategoryApply(Map conditions) {
		return goalCategoryDao.findCategoryApply(conditions);
	}

}
